//
//  DiscoverViewController.m
//  Weibo
//
//  Created by wpapple on 16/5/30.
//  Copyright © 2016年 wpapple. All rights reserved.
//

#import "DiscoverViewController.h"

@interface DiscoverViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) UITextField *textField;

@end

@implementation DiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITextField *textfield = [[UITextField alloc]init];
    
    //设置圆角边框
    textfield.borderStyle = UITextBorderStyleRoundedRect;
    textfield.frame = CGRectMake(0, 0, 320, 30);
    
    textfield.leftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"searchbar_textfield_search_icon"]];
    //左边图标默认不显示 需设置
    textfield.leftViewMode = UITextFieldViewModeAlways;
    
    textfield.clearButtonMode = UITextFieldViewModeAlways;
    
    textfield.delegate = self;
    self.textField = textfield;
    
    self.navigationItem.titleView = textfield;

}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
    
    UIImageView *icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"settings_statistic_triangle"]];
    
    icon.width = 30;
    
    //图片居中
    icon.contentMode = UIViewContentModeCenter;
    
    self.textField.leftView = icon;
    
    return YES;
}

-(void)cancel
{
    //取消显示
    self.navigationItem.rightBarButtonItem = nil;
    
    //关闭键盘
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    
    //设置左边图片
    self.textField.leftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"searchbar_textfield_search_icon"]];
}

//拖动表格  关闭键盘

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
